package com.ychug.gateway.cms;

import java.util.List;
import java.util.Map;

import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.business.service.SystemSerialNumber;
import com.ychug.service.cms.entity.CmsCategoryEntity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ychug.service.cms.service.ICmsCategoryService;
import com.ychug.framework.web.base.BaseController;


/**
 * 栏目 信息操作处理
 * 
 * @author mcpcms
 * @date 2019-04-27
 */
@Controller
@RequestMapping("/cms/category")
public class CmsCategoryController extends BaseController
{
    private String prefix = "modules/cms/category";
	
	@Autowired
	private ICmsCategoryService cmsCategoryService;

	@GetMapping()
	public String cmsCategory()
	{
	    return prefix + "/viewCategory";
	}

	@Autowired
	private SystemSerialNumber systemSerialNumber;


	@GetMapping("/list")
	@ResponseBody
	public List<CmsCategoryEntity> list(CmsCategoryEntity cmsCategory)
	{
		List<CmsCategoryEntity> list = cmsCategoryService.selectCmsCategoryList(cmsCategory);
		return list;
	}

	/**
	 * 新增栏目
	 *
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap)
	{
		mmap.put("category", cmsCategoryService.selectCmsCategoryById(parentId));
		return prefix + "/addCategory";
	}
	/**
	 * 新增保存栏目
	 */
	@PostMapping("/add")
	@ResponseBody
	public ResultAjaxHash addSave(CmsCategoryEntity cmsCategory)
	{
		cmsCategory.setSiteId(1);
		String catCode = systemSerialNumber.createSerialNumber("CatNo","CMS");
		cmsCategory.setCatCode(catCode);
		cmsCategory.setUpdateBy(getLoginName());
		return toAjax(cmsCategoryService.insertCmsCategory(cmsCategory));

	}

	/**
	 * 修改栏目
	 */
	@GetMapping("/edit/{catId}")
	public String edit(@PathVariable("catId") Integer categoryId, ModelMap mmap)
	{
		CmsCategoryEntity cmsCategory = cmsCategoryService.selectCmsCategoryById(categoryId);
		mmap.put("category", cmsCategory);
	    return prefix + "/editCategory";
	}
	
	/**
	 * 修改保存栏目
	 */
	@PostMapping("/edit")
	@ResponseBody
	public ResultAjaxHash editSave(CmsCategoryEntity cmsCategory)
	{		
		return toAjax(cmsCategoryService.updateCmsCategory(cmsCategory));
	}
	
	/**
	 * 删除栏目
	 */
	@PostMapping("/remove/{categoryId}")
	@ResponseBody
	public ResultAjaxHash remove(@PathVariable("categoryId") String ids)
	{		
		return toAjax(cmsCategoryService.deleteCmsCategoryByIds(ids));
	}


	/**
	 * 选择栏目树
	 */
	@GetMapping("/selectCateTree/{catId}")
	public String selectCateTree(@PathVariable("catId") Integer categoryId, ModelMap mmap)
	{
		CmsCategoryEntity cmsCategory = cmsCategoryService.selectCmsCategoryById(categoryId);
		mmap.put("category", cmsCategory);
		return prefix + "/tree";
	}


	/**
	 * 加载栏目列表树
	 */
	@GetMapping("/treeData")
	@ResponseBody
	public List<Map<String, Object>> treeData()
	{
		List<Map<String, Object>> tree = cmsCategoryService.selectCmsCategoryTree();
		return tree;
	}
	
}
